import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;

public class Client {

    public static void main(String args[]) {
        try {
            System.out.println("=Client process=");
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            InetAddress address =  InetAddress.getByName("localhost");
            int port = 1234;
            System.out.println("For working with data you can use next commands:" +
                    " get <surname name>/phone>," +
                    " put <surname name> <phone>," +
                    " remove <surname name>/<phone>," +
                    " update <surname name> <phone>," +
                    " exit.");
            System.out.println("For administration you can use next commands:" +
                    " add master <IP> <port> <keyFrom> <keyTo>," +
                    " add slave <IP slave> <port slave> <IP master> <port master>.");
            System.out.println("Choosed balancer:" + address + ":" + port);
            while (true) {
                try {
                    Socket socket = new Socket(address, port);
                    String query = in.readLine();
                    if (query.equals("exit")) {
                        in.close();
                        socket.close();
                        System.exit(0);
                    }
                    Serialization.writeObject(socket, query);
                    System.out.println(Serialization.readObject(socket));
                    socket.close();
                } catch (Exception e) {
                    System.out.println("Error: " + e.getMessage());
                }
            }
        } catch(Exception e) {
            System.out.println("Error: " + e);
        }
    }
}
